/**
 * 
 */
package com.seascape.swms.system.shiro;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.beetl.sql.core.SQLManager;

import com.seascape.swms.system.model.User;

/**
 * @author Lay
 *
 */
public class SimpleShiroRealm extends AuthorizingRealm{
	
	private SQLManager sqlManager;

	/* (non-Javadoc)
	 * @see org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String userName = (String)token.getPrincipal();
		User template = new User();
		template.setCode(userName);
		List<User> users = sqlManager.template(template);
		if(users.size()==1) {
			User user = users.get(0);
			return new SimpleAuthenticationInfo(user.getCode(),user.getPassword(),getName());
		}
		return null;
	}

	/**
	 * @param sqlManager the sqlManager to set
	 */
	public void setSqlManager(SQLManager sqlManager) {
		this.sqlManager = sqlManager;
	}

	
}
